package com.yj.consumer.table;

import java.util.ArrayList;
import java.util.List;

public class PageTableHandler {
    private ListHandler listHandler;
    private CountHandler countHandler;

    public PageTableHandler(ListHandler listHandler,CountHandler countHandler){
        this.listHandler = listHandler;
        this.countHandler = countHandler;
    }

    public PageTableResponse handle(PageTableRequest request){
        List<?> list = null;
        int count = this.countHandler.count(request);
        list = this.listHandler.list(request);
        if (list == null){
            list = new ArrayList<>();
        }
        if (request.getLimit() == 0 || request.getOffset() == 0){
            return new PageTableResponse(list);
        }
        return new PageTableResponse(list,request.getOffset(),request.getLimit());
    }


   interface ListHandler <T>{
        List<?> list(PageTableRequest request);
    }

    interface CountHandler{
       int count(PageTableRequest request);
    }

}
